package leetCode.secondWeek;

public class Code_66 {
    public int[] plusOne(int[] digits) {
        if (digits.length == 0) {
            return new int[0];
        }
        int pEnd = digits.length - 1;
        if (digits[pEnd] < 9) {
            digits[pEnd] = digits[pEnd] + 1;
            return digits;
        }
        int[] tmp = new int[digits.length + 1];
        int carry = 1;
        for (int i = digits.length - 1; i >= 0; i--) {
            if (digits[i] + carry >= 10) {
                tmp[i + 1] = digits[i] + carry - 10;
                digits[i] = tmp[i + 1];
                carry = 1;
            } else {
                tmp[i + 1] = digits[i] + carry;
                digits[i] = tmp[i + 1];
                carry = 0;
            }
        }
        tmp[0] = carry;
        return carry == 1 ? tmp : digits;
    }
}
